package com.flyoil.petromp.ui.activity.activity_me;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cnpc.fyutils.EditText_util;
import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.utils.Dialog.Dialog_util;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改密码界面
 */
public class SetPasswordActivity extends BaseActivity {
    private EditText edt_new, edt_confirm;
    private TextView tv_confirm;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_set_password;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTextTitleName("修改密码");
        setBackOnclickListner(mContext);
        httpModel = new HttpModel();
        edt_new = $(R.id.edt_set_pwd_new);
        edt_confirm = $(R.id.edt_set_pwd_confirm);
        EditText_util.setedittext_type_numberletters(edt_new);
        EditText_util.setedittext_type_numberletters(edt_confirm);
        tv_confirm = $(R.id.tv_set_pwd_confirm);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (edt_new.getText().toString().length() == 0) {
                    ToastUtils.show_toast("请输入新密码");
                    return;
                }
                if (edt_confirm.getText().toString().length() == 0) {
                    ToastUtils.show_toast("请再次输入密码");
                    return;
                }
                if (!edt_new.getText().toString().equals(edt_confirm.getText().toString())) {
                    ToastUtils.show_toast("两次输入密码不一致");
                    return;
                }

                setPassword();
            }
        });
    }

    /**
     * 重置密码
     */
    private void setPassword() {
        Dialog_util.start_NetworkRequests_diolog(mContext);
        Map<String, Object> map = new HashMap<>();
        map.put("newPassword", edt_confirm.getText().toString());
        httpModel.setPassword(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {

            @Override
            public void onSuccess(BaseEntity model) {
                Dialog_util.close_NetworkRequests_diolog();
                ToastUtils.show_toast(model.getMessage());
                if (model.getCode() == ConstantNetworkCode.SuccedCode) {
                    backFinish();
                }
            }

            @Override
            public void onFailure(String msg) {
            }
        });
    }
}
